//
//  SINATecSignNameDetailViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/3/22.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecSignNameDetailViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"

@interface SINATecSignNameDetailViewController ()

@property(strong,nonatomic) UIScrollView *contentScrollView;
@property (strong, nonatomic)  UILabel *noDataLabel;
@property(strong,nonatomic) NSMutableArray *stuScoreArray;
@end

@implementation SINATecSignNameDetailViewController

-(UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mainW, mainH)];
        [self.view addSubview:_contentScrollView];
    }
    
    return _contentScrollView;
}
-(UILabel *)noDataLabel{
    if (!_noDataLabel) {
        _noDataLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, mainH/2-30, mainW, 22)];
        _noDataLabel.text=@"暂无数据 点击刷新";
        _noDataLabel.textAlignment=NSTextAlignmentCenter;
        _noDataLabel.font=[UIFont systemFontOfSize:14];
        _noDataLabel.textColor=[UIColor grayColor];
        _noDataLabel.hidden=YES;
        [self.view addSubview:_noDataLabel];
    }
    return _noDataLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.stuScoreArray =[[NSMutableArray alloc]init];
    [self loadingSchoolSignSubScoreListNetData];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.noDataLabel andWithBlick:^(UIView *view) {
        [self loadingSchoolSignSubScoreListNetData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) loadingSchoolSignSubScoreListNetData{
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    //课程表id String syllabusid
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.syllabusid] forKey:@"syllabusid"];
    //课程表子表id String syllabusclassid
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.syllabusclassid] forKey:@"syllabusclassid"];
    //查询日期 String searchdate
    [dict setObject:[NSString stringWithFormat:@"%@",self.searchdate] forKey:@"searchdate"];
    
    //状态String status: 1 已签到; 0未签到
    if (self.pageIndex==1) {
        [dict setObject:[NSString stringWithFormat:@"1"] forKey:@"status"];
    } else {
        [dict setObject:[NSString stringWithFormat:@"0"] forKey:@"status"];
    }
    
    WeakSelf;
    [self.stuScoreArray removeAllObjects];
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getStudentSignInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.contentScrollView.hidden=YES;
            } else {
                //
                /*
                 0未签到
                 {
                 "collegename": "数学学院",
                 "collsystemname": "数学系",
                 "realname": "周润发",
                 "classifyname": "高数1班"
                 }
                 */
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINASubClassModel *model = [[SINASubClassModel alloc]initWithDict:dic];
                    [self.stuScoreArray addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.contentScrollView.hidden=NO;
                [self initViewFunction];
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.contentScrollView.hidden=YES;
            
        }
        
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        //DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.contentScrollView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}


/*
 
 {
 "lessonperiod": 150,
 "signcnt": 3,
 "schoolyear": "2017 - 2018",
 "term": "上半学期",
 "realname": "赵七"
 }
 */


-(void) initViewFunction{
    
    //每行列数
    NSInteger rank = 2;
    //每列间距
    CGFloat rankMargin = 0;
    //每行间距
    CGFloat rowMargin = 0;
    //Item索引 ->根据需求改变索引
    NSUInteger index = self.stuScoreArray.count;
    
    
    //每个Item宽高
    CGFloat W = (mainW-(rank-1)*rankMargin)/rank;
    CGFloat H = 68;
    
    for (NSInteger i = 0 ; i< index; i++) {
        
        SINASubClassModel *model = self.stuScoreArray[i];
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin);
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);
        //Item top
        CGFloat top = 0;
        UIView *speedView = [[UIView alloc] init];
        speedView.backgroundColor = [UIColor whiteColor];
        speedView.frame = CGRectMake(X, Y+top, W, H);
        speedView.layer.borderWidth = 0.3;
        speedView.layer.borderColor = [[UIColor colorWithHexString:@"eeeeee"] CGColor];
        
        /*
         
         {
         "lessonperiod": 150,
         "signcnt": 3,
         "schoolyear": "2017 - 2018",
         "term": "上半学期",
         "realname": "赵七"
         }
         */
        
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 21)];
        textLabel.text=[NSString stringWithFormat:@"%@",model.realname];
        textLabel.font=[UIFont systemFontOfSize:14];
        [speedView addSubview:textLabel];
        
        UILabel *scroeabel = [[UILabel alloc]initWithFrame:CGRectMake(W-66, 10,56, 26)];
        scroeabel.textAlignment=NSTextAlignmentCenter;
        [SINACircularBeadUtils makeCircularBead:4 andView:scroeabel];
        scroeabel.font=[UIFont systemFontOfSize:13];
        [speedView addSubview:scroeabel];
        
        
        if (self.pageIndex==1) {
            //签到
            scroeabel.text=@"签到";
            scroeabel.textColor=[UIColor colorWithHexString:@"#ffffff"];
            scroeabel.backgroundColor=[UIColor colorWithHexString:@"#0f77e6"];
            
            scroeabel.text=[NSString stringWithFormat:@"%@",[SINACalendarUtils getDateStringWithTimeStr:model.signTime andFormat:@"HH:mm"]];
        } else {
            //未签到
            scroeabel.text=@"未签到";
            scroeabel.textColor=[UIColor colorWithHexString:@"#878787"];
            scroeabel.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
        }
        
        
        
        UILabel *flagLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(textLabel.frame)+10,W-20, 21)];
        flagLabel.text=[NSString stringWithFormat:@"%@%@%@",model.collegename,model.collsystemname,model.classifyname];
        flagLabel.textColor=[UIColor colorWithHexString:@"#878787"];
        flagLabel.textAlignment=NSTextAlignmentLeft;
        flagLabel.font=[UIFont systemFontOfSize:13];
        [speedView addSubview:flagLabel];
        
        
        [self.contentScrollView addSubview:speedView];
        
    }
    
    self.contentScrollView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.contentScrollView.contentSize=CGSizeMake(mainW, H*(index+4)/2);
}


@end
